package com.xuxin.nantai.activity;

import android.os.Handler;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.KeyboardUtils;
import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.xuxin.nantai.R;
import com.xuxin.nantai.base.BaseActivity;
import com.xuxin.nantai.bean.BaseBean;
import com.xuxin.nantai.contract.PasswordContract;
import com.xuxin.nantai.presenter.PasswordPresenter;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by xu_xin
 * Created on 2020.06.11 0011
 */
public class PasswordActivity extends BaseActivity implements PasswordContract.View {
    private PasswordContract.Presenter presenter = new PasswordPresenter(this);
    @BindView(R.id.title_left)
    ImageView title_left;
    @BindView(R.id.title_right)
    ImageView title_right;
    @BindView(R.id.title_name)
    TextView title_name;

    @BindView(R.id.password_name)
    EditText password_name;//登录名
    @BindView(R.id.password_old)
    EditText password_old;//原密码
    @BindView(R.id.password_new)
    EditText password_new;//新密码
    @BindView(R.id.password_confirm)
    EditText password_confirm;//确认密码

    private String loginName;//登录名
    private String my_old;//原密码
    private String my_new;//新密码

    @Override
    public void setContentView() {
        setContentView(R.layout.activity_password);
    }

    @Override
    public void initView() {
        title_name.setText("密码修改");
    }

    @Override
    public void initData() {

    }

    @Override
    public void onComplete() {

    }

    @Override
    @OnClick({R.id.title_left, R.id.password_update})
    public void onClick(View view) {
        KeyboardUtils.hideSoftInput(this);
        switch (view.getId()) {
            case R.id.title_left:
                finish();
                break;
            case R.id.password_update:
                try {
                    KeyboardUtils.hideSoftInput(this);
                    loginName = password_name.getText().toString();
                    my_old = password_old.getText().toString();
                    my_new = password_new.getText().toString();
                    String confirm = password_confirm.getText().toString();
                    if (StringUtils.isEmpty(loginName)) {
                        ToastUtils.showShort("请输入登录账号");
                        return;
                    }
                    if (StringUtils.isEmpty(my_old)) {
                        ToastUtils.showShort("请输入原密码");
                        return;
                    }
                    if (StringUtils.isEmpty(my_new)) {
                        ToastUtils.showShort("请输入新密码");
                        return;
                    }
                    if (my_new.length() < 6) {
                        ToastUtils.showShort("密码不得低于6位数");
                        return;
                    }
                    if (StringUtils.isEmpty(confirm)) {
                        ToastUtils.showShort("请再次输入新密码");
                        return;
                    }
                    if (confirm.length() < 6) {
                        ToastUtils.showShort("密码不得低于6位数");
                        return;
                    }
                    if (!my_new.equals(confirm)) {
                        ToastUtils.showShort("两次密码输入不一致");
                        return;
                    }
                    handler.sendEmptyMessage(0);
                    break;
                } catch (Exception e) {
                    e.printStackTrace();
                }
        }
    }

    public Handler handler = new Handler(message -> {
        switch (message.what) {
            case 0:
                presenter.updatePassword(loginName, my_old, my_new);
                break;
        }
        return false;
    });

    @Override
    public void onSuccess(BaseBean baseBean) {
        ToastUtils.showShort(baseBean.getMessage());
        finish();
    }
}